package com.lxp.modules.gyy.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxp.common.constant.HttpStatus;
import com.lxp.common.core.page.TableDataInfo;
import com.lxp.modules.gyy.domain.HrAttendance;
import com.lxp.modules.gyy.domain.HrEmployee;
import com.lxp.modules.gyy.domain.vo.AuditParam;
import com.lxp.modules.gyy.mapper.HrAttendanceMapper;
import com.lxp.modules.gyy.service.HrAttendanceService;
import com.lxp.modules.gyy.service.HrEmployeeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【hr_attendance(Attendance)】的数据库操作Service实现
 * @createDate 2024-07-29 08:17:37
 */
@Service
public class HrAttendanceServiceImpl extends ServiceImpl<HrAttendanceMapper, HrAttendance> implements HrAttendanceService {
    @Resource
    private HrEmployeeService employeeService;

    @Override
    public TableDataInfo pageList(AuditParam param, TableDataInfo info) {
        LambdaQueryWrapper<HrAttendance> lqw = Wrappers.lambdaQuery();
        Date date = new Date();
        if (param.getStartDate() != null) {
            Date jdkDate = DateUtil.beginOfMonth(date).toJdkDate();
            lqw.ge(HrAttendance::getCreateDate, jdkDate);
        }
        if (param.getEndDate() != null) {
            Date jdkDate = DateUtil.endOfMonth(date).toJdkDate();
            lqw.le(HrAttendance::getCreateDate, jdkDate);
        }
        Integer userId = param.getUserId();
        if (userId != null) {
            lqw.eq(HrAttendance::getEmployeeId, userId);
        }
        Long count = this.baseMapper.selectCount(lqw);
        info.setTotal(count);
        lqw.orderByDesc(HrAttendance::getCreateDate);
        IPage<HrAttendance> page = new Page<>(info.getPageNum(), info.getPageSize());
        IPage<HrAttendance> orderIPage = this.baseMapper.selectPage(page, lqw);
        info.setRows(setName(orderIPage.getRecords()));
        info.setCode(HttpStatus.SUCCESS);
        return info;
    }

    private List<HrAttendance> setName(List<HrAttendance> list) {
        List<HrAttendance> list1 = new ArrayList<>();
        for (HrAttendance hrAttendance : list) {
            HrEmployee employee = employeeService.getById(hrAttendance.getEmployeeId());
            if (employee != null) {
                hrAttendance.setEmpName(employee.getName());
            }
            list1.add(hrAttendance);
        }
        return list1;
    }
}




